package lesson11;

import java.util.concurrent.Semaphore;

public class Demo_1101 {
    public static void main(String[] args) {
        Semaphore semaphore=new Semaphore(5);
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"开始申请资源");
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"已经申请到资源");
                    semaphore.release();
                    System.out.println(Thread.currentThread().getName()+"已释放资源");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        };
        for (int i = 0; i < 20; i++) {
            Thread thread=new Thread(runnable);
            thread.start();

        }
    }
}
